#include "PlaylistVisualList.h"

#include"../Song/SongData.h"

#include<QMessageBox>

PlaylistVisualList::PlaylistVisualList(QWidget *parent) : QListWidget(parent){
	setAcceptDrops(true);
	connect(this, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(doubleClicked(QListWidgetItem*)));
}

#include<stdio.h>
void PlaylistVisualList::insert(int location, SongData *data){
	printf("Insertion...\n");
	QListWidgetItem *item = new QListWidgetItem( data->title, this );
	printf("Adding song: %s\n", data->title.toUtf8().data());
	addItem(item);
	printf("Inserted %s\n", data->title.toUtf8().data());
}

void PlaylistVisualList::doubleClicked(QListWidgetItem *item){
	printf("Clicked row %d\n", row(item));
	emit play( row(item) );
}

void PlaylistVisualList::dragEnterEvent(QDragEnterEvent *event){
	const QMimeData *data = event->mimeData();
	if(data->hasText() || data->hasUrls()){
		event->accept();
	}
}

void PlaylistVisualList::dragMoveEvent(QDragMoveEvent *event){
	event->accept();
}

void PlaylistVisualList::dragLeaveEvent(QDragLeaveEvent *event){
	event->ignore();
}

void PlaylistVisualList::dropEvent(QDropEvent *event){
	event->acceptProposedAction();
	const QMimeData *data = event->mimeData();
	if(data->hasUrls()){
		QList<QUrl> urls = data->urls();
		emit addSongs(urls, 0);
		for(int i = 0; i < urls.size(); i++)
			printf("Urls: %s\n", urls[i].toLocalFile().toUtf8().data());
	}else{
		QMessageBox::warning(this, "Warning", "Something went horribly wrong with your drag and drop. We somehow did not receive any URLs.");
	}
}
